<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of menu
 *
 * @author Dmitry
 */
require_once 'app/access/db.php';
class Models_Menu extends Access_Db {
    //put your code here
    protected $query ;
    var $id ;
    private $registry ;
    function __construct($registrty) {
        $this->registry = $registrty ;
    }

    public function getMenu()
    {
        $this->query = "SELECT ID, name FROM yyMenu WHERE visible=1" ;
        $result = $this->executeQuery($this->query) ;
        while ($row = mysql_fetch_array($result))
        {
            $menu[$row['ID']] = $row['name'] ;
        }
        return $menu ;
    }

    public function getSubMenu($id)
    {
        settype($id, 'integer') ;
        $this->query = "SELECT yyProducts.categoryID, yyCategories.name 
                FROM yyCategories, yyProducts 
                WHERE yyCategories.visible=1 and yyCategories.menuID=$id and
                yyCategories.ID=yyProducts.categoryID" ;
        $result = $this->executeQuery($this->query) ;
        
        while ($row = mysql_fetch_array($result))
        {
            $subMenu[$row['categoryID']] = $row['name'] ;
        }
        if (!isset ($subMenu)) { return false ; }
        return $subMenu ;
    }
    
    public function getSubMenuUri($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery("SELECT uri FROM yyCategories WHERE ID=$id") ;
        $row = mysql_fetch_assoc($result) ;
        return $row['uri'] ;
    }
    
    public function getMenuUri($id)
    {
        settype($id, 'integer') ;
        $result = $this->executeQuery("SELECT uri FROM yyMenu WHERE ID=$id") ;
        $row = mysql_fetch_assoc($result) ;
        return $row['uri'] ;
    }
    
    public function showMenu()
    {
        if(isset ($_SESSION['admHash']) and isset ($_COOKIE['hash']) and $_SESSION['admHash'] == $_COOKIE['hash'])
        {
            echo '<ul id="menu">
                    <li><a href="/' . $this->path . 'adm/showallcategories">Категорії</a>
                        <ul id="submenu">
                            <li><a href="/' . $this->path . 'adm/addcategory">Додати категорію</a></li>
                            <li><a href="/' . $this->path . 'adm/showallcategories">Редагувати/Видалити категорію</a></li>
                    </ul></li>
                    <li><a href="/' . $this->path . 'adm/showallgoods">Товари</a>
                        <ul id="submenu">
                            <li><a href="/' . $this->path . 'adm/addgoods">Додати товар</a></li>
                            <li><a href="/' . $this->path . 'adm/showallgoods">Редагувати/Видалити товар</a></li>
                            <li><a href="/' . $this->path . 'adm/showproperties">Властивості</a></li>
                    </ul></li>
                    <li><a href="/' . $this->path . 'adm/showallusers">Користувачі</a>
                    </li>
                    <li><a href="/' . $this->path . 'adm/showorders">Замовлення</a></li>
                  </ul>' ;
            
        } else {
            echo '<ul id="menu">' ;
            $menu = $this->getMenu() ;
            foreach ($menu as $menuID => $menuName)
            {

                echo '<li><a href="/'. $this->path . 'products/all/' .$menuID.'">' . $menuName . '</a>' ;
                $subMenu = $this->getSubMenu($menuID) ;
                if ($subMenu) {
                    echo '<ul id="submenu">' ;

                    foreach ($subMenu as $subMenuID => $subMenuName)
                    {
                        echo '<li><a href="'. $this->registry->get('sitePath') .
                               'products/category/' . $subMenuID.'">' . $subMenuName . '</a></li>';
                    }
                echo '</ul></li>' ;
                }

            }
            echo '</ul>' ;
        }
        
        
    }
}
?>
